<?php
class CompanyTypesController extends AppController {

    public $name = 'CompanyTypes';

    public function index(){
        $this->CompanyType->recursive = 0;
        $this->set('status', '');
        $this->set('companyTypes', $this->paginate());
    }

    public function view($id = null){
        if(!$id){
            $this->set('status',__('Invalid CompanyType.', true));
            $this->redirect(array('action'=>'index'));
        }
        $this->Response->ajax();
        $this->CompanyType->recursive = 1;
        $this->set('status', '');
        $this->set('companyType', $this->CompanyType->read(null, $id));
    }

    public function add(){
        if(!empty($this->data)){
            $this->CompanyType->create();
            if($this->CompanyType->save($this->data)){
                $this->Session->setFlash(__('The CompanyType has been saved', true));
            }else{
                $this->Session->setFlash(__('The CompanyType could not be saved. Please, try again.', true));
            }
        }
        $this->redirect(array('action'=>'index'));
    }

    public function edit($id = null){
        if(!$id && empty($this->data)){
            $this->set('status',__('Invalid CompanyType', true));
            $this->redirect(array('action'=>'index'));
        }
        if(!empty($this->data)){
            if($this->CompanyType->save($this->data)){
                $this->Session->setFlash(__('The CompanyType has been saved', true));
            }else{
                $this->Session->setFlash(__('The CompanyType could not be saved. Please, try again.', true));
            }
            $this->redirect(array('action'=>'index'));
        }
        if(empty($this->data)){
            $this->data = $this->CompanyType->read(null, $id);
            $this->set('status', '');
            $this->Response->ajax();
        }
    }

    public function delete($id = null){
        if(!$id){
            $this->Response->json(false, __('Invalid id for CompanyType', true));
        }
        if($this->CompanyType->del($id)){
            $this->Response->json(true, __('CompanyType deleted', true));
        }
    }

    public function check(){
        if(!empty($this->data)){
            $term = $this->CompanyType->findByName($this->data['CompanyType']['name']);
            if(empty($term['CompanyType']['name'])){
                $this->Response->json(true, 'CompanyType name check success', $term, null);
            }else{
                $this->Response->json(false, 'CompanyType name check failure', $term, null);
            }
        }else{
            $this->Response->json(false, 'Invalid CompanyType name check request', null, null);
        }
    }

}
?>